//
//  MPCSearchBar.m
//  搜索栏定制
//
//  Created by LG on 2017/11/29.
//  Copyright © 2017年 my. All rights reserved.
//

#import "MPCSearchBar.h"
#import "Masonry.h"

@interface MPCSearchBar ()
<
UITextFieldDelegate
>

@property (nonatomic, strong) UIView *view_inputContent;

@property (nonatomic, strong) UITextField *textFiled_input;

@property (nonatomic, strong) UIView *view_inputLeft;
@property (nonatomic, strong) UIImageView *imgView_ico;

@property (nonatomic, strong) UIButton *btn_right;

@property (nonatomic, strong) UIControl *ctrl_Cover;

@end

@implementation MPCSearchBar

#pragma mark - Inherit

- (instancetype)initWithSearchBarType:(MPCSearchBarType)type;
{
    self = [super init];
    if (self) {
        [self initUIWithSearchBarType:type];
    }
    return self;
}

#pragma mark - UI

- (void)initUIWithSearchBarType:(MPCSearchBarType)type {
    self.view_inputContent = ({
        UIView *view = [[UIView alloc]init];
        view.layer.cornerRadius = 15.;
        view.layer.masksToBounds = YES;
        [self addSubview:view];
        view;
    });
    
    self.textFiled_input = ({
        UITextField *textField = [[UITextField alloc]init];
        textField.delegate = self;
        [self.view_inputContent addSubview:textField];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField;
    });
    
    self.btn_right = ({
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:kSearchBarRightButtonDefault forState:UIControlStateNormal];
        [btn setTitleColor:COLOR_WITH_HEX(0x25252C) forState:UIControlStateNormal];
        [btn setTitleColor:COLOR_WITH_HEX(0x97979C) forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(btnClick_right:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        btn;
    });
    
    self.imgView_ico = ({
        UIImageView *imgView = [[UIImageView alloc]init];
        UIImage *image = [UIImage imageNamed:kSearchDefaultImageName];
        imgView.image = image;
        imgView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        imgView;
    });
    
    self.view_inputLeft = ({
        UIView *view = [[UIView alloc]init];
        [view addSubview:self.imgView_ico];
        CGRect frame = self.imgView_ico.frame;
        frame.size.width += 10;
        view.frame = frame;
        self.textFiled_input.leftView = view;
        self.textFiled_input.leftViewMode = UITextFieldViewModeAlways;
        view;
    });
    
    self.ctrl_Cover = ({
        UIControl *ctrl = [[UIControl alloc]init];
        [ctrl addTarget:self action:@selector(btnClick_cover:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:ctrl];
        ctrl;
    });
    
    self.searchBarType = type;
    
    [self layoutViews];
}

- (void)layoutViews {
    [self.btn_right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        if (!self.isRightBtnUseable) {
            make.width.mas_equalTo(0);
            make.right.equalTo(self).with.offset(0);
        } else {
            make.width.mas_offset(40);
            make.right.equalTo(self).with.offset(-15);
        }
    }];
    
    CGFloat spaceToCancle = self.isRightBtnUseable ? 23. : 15.;
    [self.view_inputContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btn_right.mas_left).with.offset(-spaceToCancle);
        make.centerY.equalTo(self);
        make.left.equalTo(self).with.offset(15.);
        make.height.mas_equalTo(30.);
    }];
    
    [self.textFiled_input mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view_inputContent);
        make.left.equalTo(self.view_inputContent).with.offset(10);
        make.right.equalTo(self.view_inputContent).with.offset(-10);
    }];
    
    [self.ctrl_Cover mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

#pragma mark - Public
- (void)searchBarBecomeFirstResponse:(BOOL)becomeFirst {
    if (becomeFirst) {
        [self.textFiled_input becomeFirstResponder];
    } else {
        [self.textFiled_input resignFirstResponder];
    }
}

#pragma mark - Private Methods

#pragma mark - Delegate
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(searchBarShouldBeginEditing:textfield:)]) {
       return [self.delegate searchBarShouldBeginEditing:self textfield:textField];
    }
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(searchBarDidBeginEditing:textfield:)]) {
        [self.delegate searchBarDidBeginEditing:self textfield:textField];
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(searchBarShouldEndEditing:textfield:)]) {
        return [self.delegate searchBarShouldEndEditing:self textfield:textField];
    }
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(searchBarDidEndEditing:textfield:)]) {
        [self.delegate searchBarDidEndEditing:self textfield:textField];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason {
    if ([self.delegate respondsToSelector:@selector(searchBarDidEndEditing:textfield:reason:)]) {
        [self.delegate searchBarDidEndEditing:self textfield:textField reason:reason];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([self.delegate respondsToSelector:@selector(searchBar:textfield:shouldChangeCharactersInRange:replacementString:)]) {
       return [self.delegate searchBar:self textfield:textField shouldChangeCharactersInRange:range replacementString:string];
    }
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(searchBarShouldClear:textfield:)]) {
        return [self.delegate searchBarShouldClear:self textfield:textField];
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(searchBarShouldReturn:textfield:)]) {
        return [self.delegate searchBarShouldReturn:self textfield:textField];
    }
    
    return YES;
}

#pragma mark - Actions

/**
 右侧按钮点击事件

 @param aSender x
 */
- (void)btnClick_right:(UIButton*)aSender {
    if (self.rightHandler) {
        self.rightHandler(self.textFiled_input.text);
    }
}

/**
 当 searchBarType = MPCSearchBarTypeButton 时，当前控件的点击事件

 @param aSender x
 */
- (void)btnClick_cover:(UIControl*)aSender {
    if (self.handler) {
        self.handler();
    }
}

#pragma mark - Getter/Setter

- (void)setSearchBarType:(MPCSearchBarType)searchBarType {
    _searchBarType = searchBarType;
    self.ctrl_Cover.hidden = searchBarType == MPCSearchBarTypeDefault;
}

- (void)setIsRightBtnUseable:(BOOL)isRightBtnUseable {
    _isRightBtnUseable = isRightBtnUseable;
    [self.btn_right mas_updateConstraints:^(MASConstraintMaker *make) {
        if (!self.isRightBtnUseable) {
            make.width.mas_equalTo(0);
            make.right.equalTo(self).with.offset(0);
        } else {
            make.width.mas_offset(40);
            make.right.equalTo(self).with.offset(-15);
        }
    }];
    
    CGFloat spaceToCancle = self.isRightBtnUseable ? 23. : 15.;
    [self.view_inputContent mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btn_right.mas_left).with.offset(-spaceToCancle);
    }];
}

- (void)setLeftSearchImageName:(NSString *)leftSearchImageName {
    _leftSearchImageName = leftSearchImageName;
    UIImage *image = [UIImage imageNamed:leftSearchImageName];
    self.imgView_ico.image = image;
    CGRect imgViewFrame = CGRectMake(0, 0, image.size.width, image.size.height);
    self.imgView_ico.frame = imgViewFrame;
    imgViewFrame.size.width += 5;
    self.textFiled_input.leftView.frame = imgViewFrame;
    self.textFiled_input.leftViewMode = UITextFieldViewModeAlways;
}

- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    [self.btn_right setTitle:rightTitle forState:UIControlStateNormal];
}

- (void)setClearViewMode:(UITextFieldViewMode)clearViewMode {
    _clearViewMode = clearViewMode;
    self.textFiled_input.clearButtonMode = clearViewMode;
}

- (void)setPlaceholdText:(NSString *)placeholdText {
    _placeholdText = placeholdText;
    self.textFiled_input.placeholder = placeholdText;
}

- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder {
    _attributedPlaceholder = attributedPlaceholder;
    self.textFiled_input.attributedPlaceholder = attributedPlaceholder;
}

- (void)setText:(NSString *)text {
    _text = text;
    self.textFiled_input.text = text;
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    _attributedText = attributedText;
    self.textFiled_input.attributedText = attributedText;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    self.textFiled_input.textColor = textColor;
}

- (void)setFont:(UIFont *)font {
    _font = font;
    self.textFiled_input.font = font;
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment {
    _textAlignment = textAlignment;
    self.textFiled_input.textAlignment = textAlignment;
}

- (void)setBorderStyle:(UITextBorderStyle)borderStyle {
    _borderStyle = borderStyle;
    self.textFiled_input.borderStyle = borderStyle;
}

- (void)setInputAreaBackgroundColor:(UIColor *)inputAreaBackgroundColor {
    _inputAreaBackgroundColor = inputAreaBackgroundColor;
    self.view_inputContent.backgroundColor = inputAreaBackgroundColor;
}

@end
